<template>
    <el-container class="h-100">
        <el-main class="h-100">

            <el-row class="h-100" type="flex" justify="center" align="middle">

                <el-col :span="6" class="login-layout">
                    <h1 class="text-center">四川省水务管理系统</h1>
                    <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="80px">
                        <el-form-item label="phone" prop="phone">
                            <el-input v-model="ruleForm.phone" type="tel" autocomplete="off" />
                        </el-form-item>
                        <el-form-item label="password" prop="password">
                            <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm(ruleFormRef)">Login</el-button>
                            <el-button @click="resetForm(ruleFormRef)">找回密码</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </el-main>
    </el-container>
</template>
  
<script setup>
import { ElMessage } from 'element-plus'
import { login } from '../../config/api';
import { reactive } from 'vue';
import { ref } from 'vue'
import { useRouter } from 'vue-router';
// pinia 再组件中的使用：
import { useTokenStore } from "./../../store/index.js"
const store = useTokenStore()
// console.log(store);
const ruleFormRef = ref('ruleFormRef')
const ruleForm = reactive({
    phone: '1',
    password: '1'
})

const router = useRouter();
const rules = {
    phone: [
        { required: true, message: 'Please input phone', trigger: 'blur' },
        { min: 1, max: 11, message: 'Length should be 11', trigger: 'blur' },
    ],
    password: [
        {
            required: true, message: 'Please input password', trigger: 'blur',
        },
        { min: 1, max: 6, message: 'Length should be 1 to 6', trigger: 'blur' },
    ],
}

const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            login({ ...ruleForm })
                .then(res => {
                    console.log(res);
                    if (res.code === 200) {
                        ElMessage({
                            message: '登录成功', type: 'success', duration: 1000
                        })
                    } else {
                        ElMessage({
                            message: '登录失败', type: 'error', duration: 2000
                        })
                    }

                    sessionStorage.token = res.data.Authorization
                    sessionStorage.userInfo = JSON.stringify(res.data.admin)

                    // 使用$patch()，传对象，对象传入后，自动识别属性，再赋值给同名的State。
                    store.$patch({
                        // token: res.data.Authorization,
                        userInfo: res.data.admin
                    })

                    // STORE的actions的使用，把actions里面的方法，单stroe的函数调用就可以了。
                    // actions里面支持异步功能。
                    store.setToken(res.data.Authorization)
                    router.push("/home")

                })
        } else {
            console.log('error submit!', fields)
        }
    })
}

</script>
  
<style></style>